<?php
/**
 * @author Mazvv
 * @package Cities
 */
class Calls extends CRM_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'calls';
		$this->lang->load('calls');
		$this->load->model('calls_model');
		$this->load->model('currencies_model');
		$this->load->model('advertisessources_model');
		$this->load->model('callsstatuses_model');
		$this->SetNewToolSize('700x650');
		$this->SetEditToolSize('700x650');
		$this->SetDetailsToolSize('700x650');
		$this->SetFindToolSize('700x650');
		
		$this->a_validation_rules['_callsstatuses_rid'] = "required";       
		$this->a_validation_rules['date_doc'] = "required";
		$this->a_validation_rules['_countries_rid'] = "required";         
		$this->a_validation_rules['_callhotelcats_rid'] = "required";         
		$this->a_validation_rules['_advertisessources_rid'] = "required";         
		$this->a_validation_rules['_currencies_rid'] = "required";         
		$this->a_validation_rules['f_name'] = "required";         
		//$this->a_validation_rules['s_name'] = "required";         
		//$this->a_validation_rules['l_name'] = "required";         
		$this->a_validation_rules['summ_wanted_from'] = "required";         
		$this->a_validation_rules['summ_wanted_to'] = "required";         
		$this->a_validation_rules['tourists_quan'] = "required";         
		$this->a_validation_rules['tourists_wish'] = "required";         
		$this->a_validation_rules['tourists_offers'] = "required";         
		$this->a_validation_rules['tourists_answers'] = "required";         	
			
		$this->a_validation_fields['date_doc'] = $this->lang->line('DATE_DOC');
		$this->a_validation_fields['_callsstatuses_rid'] = $this->lang->line('CALLSTATUS');
		$this->a_validation_fields['_calls_headers_rid'] = $this->lang->line('DESCR');
		$this->a_validation_fields['_countries_rid'] = $this->lang->line('COUNTRY'); 
		$this->a_validation_fields['_callhotelcats_rid'] = $this->lang->line('HOTELCAT'); 
		$this->a_validation_fields['_clients_rid'] = $this->lang->line('CLIENT'); 
		$this->a_validation_fields['_advertisessources_rid'] = $this->lang->line('ADVERTISE'); 
		$this->a_validation_fields['_currencies_rid'] = $this->lang->line('CURRENCY'); 
		$this->a_validation_fields['f_name'] = $this->lang->line('F_NAME'); 
		$this->a_validation_fields['s_name'] = $this->lang->line('S_NAME'); 
		$this->a_validation_fields['l_name'] = $this->lang->line('L_NAME'); 
		$this->a_validation_fields['summ_wanted_from'] = $this->lang->line('SUMM_FROM'); 
		$this->a_validation_fields['summ_wanted_to'] = $this->lang->line('SUMM_TO'); 
		$this->a_validation_fields['tourists_quan'] = $this->lang->line('TOURISTS_QUAN'); 
		$this->a_validation_fields['tourists_wish'] = $this->lang->line('TOURISTS_WISH'); 
		$this->a_validation_fields['tourists_offers'] = $this->lang->line('TOURISTS_OFFERS'); 
		$this->a_validation_fields['tourists_answers'] = $this->lang->line('TOURISTS_ANSWERS'); 
		$this->a_validation_fields['date_from'] = $this->lang->line('DATE_FROM'); 		
		$this->a_validation_fields['date_to'] = $this->lang->line('DATE_TO'); 	
		$this->a_validation_fields['phones'] = $this->lang->line('PHONES'); 		
		$this->a_validation_fields['email'] = $this->lang->line('EMAIL'); 			
		
		
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_cif_name){
		switch ($m_cif_name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}
			case 'gd':{$this->_gdemand();break;}
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}

	public function GetTools(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$tools = parent::GetTools();
		$tools['gdemand'] = $this->load->view('calls/tools/gdemand', array('callrid'=>$this->GetSessionData('activerecord')), True);
        return $tools;
	}
	
	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 

		$content['date_doc'] = $this->lang->line('DATE_DOC');
		$content['call_status'] = $this->lang->line('CALLSTATUS');
		$content['country_name'] = $this->lang->line('COUNTRY'); 
		$content['hotel_cat'] = $this->lang->line('HOTELCAT'); 
		$content['source_name'] = $this->lang->line('ADVERTISE'); 
		$content['currency_code'] = $this->lang->line('CURRENCY'); 
		$content['f_name'] = $this->lang->line('F_NAME'); 
		$content['s_name'] = $this->lang->line('S_NAME'); 
		$content['l_name'] = $this->lang->line('L_NAME'); 
		$content['summ_wanted_from'] = $this->lang->line('SUMM_FROM'); 
		$content['summ_wanted_to'] = $this->lang->line('SUMM_TO'); 
		$content['tourists_quan'] = $this->lang->line('TOURISTS_QUAN'); 
		$content['date_from'] = $this->lang->line('DATE_FROM'); 		
		$content['date_to'] = $this->lang->line('DATE_TO'); 		
		
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->calls_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if($content['DS'] && !$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('calls/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE');
			
			# { date_doc
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$data = array('name'=>'date_doc', 'id'=>'date_doc', 'value'=>$this->validation->date_doc?$this->validation->date_doc:date('d.m.Y'), 'size'=>'12', 'readonly'=>'readonly');
			$content['date_doc_field'] =  form_input($data);
			# } date_doc
			
			# { _callsstatuses_rid
			$content['_callsstatuses_rid'] = $this->RenderLabel('_callsstatuses_rid');
			$content['_callsstatuses_rid_field'] =  form_dropdown('_callsstatuses_rid', $this->callsstatuses_model->GetPairArr(), $this->validation->_callsstatuses_rid?$this->validation->_callsstatuses_rid:'');
			# } _callsstatuses_rid
			
			# { _countries_rid
			$countries = array(' '=>'');
			foreach($this->calls_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid'] = $this->RenderLabel('_countries_rid');
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries, $this->validation->_countries_rid?$this->validation->_countries_rid:'');
			# } _countries_rid
			
			# { _callhotelcats_rid
			$hotelscats = array(' '=>'');
			foreach($this->calls_model->GetCallhotelcatsList() as $hotelcat){
				$hotelscats[$hotelcat->rid] = $hotelcat->callhotelcat_name;
			}
			$content['_callhotelcats_rid'] = $this->RenderLabel('_callhotelcats_rid');
			$content['_callhotelcats_rid_field'] =  form_dropdown('_callhotelcats_rid', $hotelscats, $this->validation->_callhotelcats_rid?$this->validation->_callhotelcats_rid:'');
			# { _callhotelcats_rid
			
			# { _advertisessources_rid
			$content['_advertisessources_rid'] = $this->RenderLabel('_advertisessources_rid');
			$content['_advertisessources_rid_field'] =  form_dropdown('_advertisessources_rid', $this->advertisessources_model->GetPairArr(), $this->validation->_advertisessources_rid?$this->validation->_advertisessources_rid:'');
			# } _advertisessources_rid
			
			# { _currencies_rid
			$content['_currencies_rid'] = $this->RenderLabel('_currencies_rid');
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $this->currencies_model->GetPairArr(), $this->validation->_currencies_rid?$this->validation->_currencies_rid:'');
			# } _currencies_rid
			
			# { f_name
			$content['f_name'] = $this->RenderLabel('f_name');
			$data = array('name'=>'f_name', 'id'=>'f_name', 'value'=>$this->validation->f_name?$this->validation->f_name:'', 'size'=>'20');
			$content['f_name_field'] =  form_input($data);
			# } f_name
			
			# { s_name
			$content['s_name'] = $this->RenderLabel('s_name');
			$data = array('name'=>'s_name', 'id'=>'s_name', 'value'=>$this->validation->s_name?$this->validation->s_name:'', 'size'=>'20');
			$content['s_name_field'] =  form_input($data);
			# } s_name
			
			# { l_name
			$content['l_name'] = $this->RenderLabel('l_name');
			$data = array('name'=>'l_name', 'id'=>'l_name', 'value'=>$this->validation->l_name?$this->validation->l_name:'', 'size'=>'20');
			$content['l_name_field'] =  form_input($data);
			# } l_name
			
			# { summ_wanted_from
			$content['summ_wanted_from'] = $this->RenderLabel('summ_wanted_from');
			$data = array('name'=>'summ_wanted_from', 'id'=>'summ_wanted_from', 'value'=>$this->validation->summ_wanted_from?$this->validation->summ_wanted_from:'', 'size'=>'12');
			$content['summ_wanted_from_field'] =  form_input($data);
			# } summ_wanted_from
			
			# { summ_wanted_to
			$content['summ_wanted_to'] = $this->RenderLabel('summ_wanted_to');
			$data = array('name'=>'summ_wanted_to', 'id'=>'summ_wanted_to', 'value'=>$this->validation->summ_wanted_to?$this->validation->summ_wanted_to:'', 'size'=>'12');
			$content['summ_wanted_to_field'] =  form_input($data);
			# } summ_wanted_to
			
			# { tourists_quan
			$content['tourists_quan'] = $this->RenderLabel('tourists_quan');
			$data = array('name'=>'tourists_quan', 'id'=>'tourists_quan', 'value'=>$this->validation->tourists_quan?$this->validation->tourists_quan:'', 'size'=>'12');
			$content['tourists_quan_field'] =  form_input($data);
			# } tourists_quan
			
			# { tourists_wish
			$content['tourists_wish'] = $this->RenderLabel('tourists_wish');
			$data = array('name'=>'tourists_wish', 'id'=>'tourists_wish', 'value'=>$this->validation->tourists_wish?$this->validation->tourists_wish:'', 'cols'=>'30', 'rows'=>'2');
			$content['tourists_wish_field'] =  form_textarea($data);
			# } tourists_wish
			
			# { tourists_offers
			$content['tourists_offers'] = $this->RenderLabel('tourists_offers');
			$data = array('name'=>'tourists_offers', 'id'=>'tourists_offers', 'value'=>$this->validation->tourists_offers?$this->validation->tourists_offers:'', 'cols'=>'83', 'rows'=>'2');
			$content['tourists_offers_field'] =  form_textarea($data);;
			# } tourists_offers
			
			# { tourists_answers
			$content['tourists_answers'] = $this->RenderLabel('tourists_answers');
			$data = array('name'=>'tourists_answers', 'id'=>'tourists_answers', 'value'=>$this->validation->tourists_answers?$this->validation->tourists_answers:'', 'cols'=>'30', 'rows'=>'2');
			$content['tourists_answers_field'] =  form_textarea($data);
			# } tourists_answers

			# { phones
			$content['phones'] = $this->RenderLabel('phones');
			$data = array('name'=>'phones', 'id'=>'phones', 'value'=>$this->validation->phones?$this->validation->phones:'', 'size'=>'20');
			$content['phones_field'] =  form_input($data);
			# } phones
			
			# { email
			$content['email'] = $this->RenderLabel('email');
			$data = array('name'=>'email', 'id'=>'email', 'value'=>$this->validation->email?$this->validation->email:'', 'size'=>'20');
			$content['email_field'] =  form_input($data);
			# } email
			
			# { date_from
			$content['date_from'] = $this->RenderLabel('date_from');
			$data = array('name'=>'date_from', 'id'=>'date_from', 'value'=>$this->validation->date_from?$this->validation->date_from:'', 'size'=>'12', 'readonly'=>'readonly');		
			$content['date_from_field'] =  form_input($data).$this->RenderCalendar('date_from');
			# } date_from
			
			# { date_to
			$content['date_to'] = $this->RenderLabel('date_to');
			$data = array('name'=>'date_to', 'id'=>'date_to', 'value'=>$this->validation->date_to?$this->validation->date_to:'', 'size'=>'12', 'readonly'=>'readonly');		
			$content['date_to_field'] =  form_input($data).$this->RenderCalendar('date_to');
			# } date_to
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('calls/create', $content, True);
		}
		if($insRid = $this->calls_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->calls_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT');
			
			# { date_doc
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$data = array('name'=>'date_doc', 'id'=>'date_doc', 'value'=>$this->validation->date_doc?$this->validation->date_doc:$content['DS']->date_doc, 'size'=>'12', 'readonly'=>'readonly');
			$content['date_doc_field'] =  form_input($data);
			# } date_doc
			
			# { _callsstatuses_rid
			$content['_callsstatuses_rid'] = $this->RenderLabel('_callsstatuses_rid');
			$content['_callsstatuses_rid_field'] =  form_dropdown('_callsstatuses_rid', $this->callsstatuses_model->GetPairArr(), $this->validation->_callsstatuses_rid?$this->validation->_callsstatuses_rid:$content['DS']->_callsstatuses_rid);
			# } _callsstatuses_rid
			
			# { _countries_rid
			$countries = array(' '=>'');
			foreach($this->calls_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid'] = $this->RenderLabel('_countries_rid');
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries, $this->validation->_countries_rid?$this->validation->_countries_rid:$content['DS']->_countries_rid);
			# } _countries_rid
			
			# { _callhotelcats_rid
			$hotelscats = array(' '=>'');
			foreach($this->calls_model->GetCallhotelcatsList() as $hotelcat){
				$hotelscats[$hotelcat->rid] = $hotelcat->callhotelcat_name;
			}
			$content['_callhotelcats_rid'] = $this->RenderLabel('_callhotelcats_rid');
			$content['_callhotelcats_rid_field'] =  form_dropdown('_callhotelcats_rid', $hotelscats, $this->validation->_callhotelcats_rid?$this->validation->_callhotelcats_rid:$content['DS']->_callhotelcats_rid);
			# { _callhotelcats_rid
			
			# { _advertisessources_rid
			$content['_advertisessources_rid'] = $this->RenderLabel('_advertisessources_rid');
			$content['_advertisessources_rid_field'] =  form_dropdown('_advertisessources_rid', $this->advertisessources_model->GetPairArr(), $this->validation->_advertisessources_rid?$this->validation->_advertisessources_rid:$content['DS']->_advertisessources_rid);
			# } _advertisessources_rid
						
			# { _currencies_rid
			$content['_currencies_rid'] = $this->RenderLabel('_currencies_rid'); 
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $this->currencies_model->GetPairArr(), $this->validation->_currencies_rid?$this->validation->_currencies_rid:$content['DS']->_currencies_rid);
			# } _currencies_rid
			
			# { f_name
			$content['f_name'] = $this->RenderLabel('f_name');
			$data = array('name'=>'f_name', 'id'=>'f_name', 'value'=>$this->validation->f_name?$this->validation->f_name:$content['DS']->f_name, 'size'=>'20');
			$content['f_name_field'] =  form_input($data);
			# } f_name
			
			# { s_name
			$content['s_name'] = $this->RenderLabel('s_name');
			$data = array('name'=>'s_name', 'id'=>'s_name', 'value'=>$this->validation->s_name?$this->validation->s_name:$content['DS']->s_name, 'size'=>'20');
			$content['s_name_field'] =  form_input($data);
			# } s_name
			
			# { l_name
			$content['l_name'] = $this->RenderLabel('l_name');
			$data = array('name'=>'l_name', 'id'=>'l_name', 'value'=>$this->validation->l_name?$this->validation->l_name:$content['DS']->l_name, 'size'=>'20');
			$content['l_name_field'] =  form_input($data);
			# } l_name
			
			# { summ_wanted_from
			$content['summ_wanted_from'] = $this->RenderLabel('summ_wanted_from');
			$data = array('name'=>'summ_wanted_from', 'id'=>'summ_wanted_from', 'value'=>$this->validation->summ_wanted_from?$this->validation->summ_wanted_from:$content['DS']->summ_wanted_from, 'size'=>'12');
			$content['summ_wanted_from_field'] =  form_input($data);
			# } summ_wanted_from
			
			# { summ_wanted_to
			$content['summ_wanted_to'] = $this->RenderLabel('summ_wanted_to');
			$data = array('name'=>'summ_wanted_to', 'id'=>'summ_wanted_to', 'value'=>$this->validation->summ_wanted_to?$this->validation->summ_wanted_to:$content['DS']->summ_wanted_to, 'size'=>'12');
			$content['summ_wanted_to_field'] =  form_input($data);
			# } summ_wanted_to
			
			# { tourists_quan
			$content['tourists_quan'] = $this->RenderLabel('tourists_quan');
			$data = array('name'=>'tourists_quan', 'id'=>'tourists_quan', 'value'=>$this->validation->tourists_quan?$this->validation->tourists_quan:$content['DS']->tourists_quan, 'size'=>'12');
			$content['tourists_quan_field'] =  form_input($data);
			# } tourists_quan
			
			# { tourists_wish
			$content['tourists_wish'] = $this->RenderLabel('tourists_wish');
			$data = array('name'=>'tourists_wish', 'id'=>'tourists_wish', 'value'=>$this->validation->tourists_wish?$this->validation->tourists_wish:$content['DS']->tourists_wish, 'cols'=>'30', 'rows'=>'2');
			$content['tourists_wish_field'] =  form_textarea($data);
			# } tourists_wish
			
			# { tourists_offers
			$content['tourists_offers'] = $this->RenderLabel('tourists_offers');
			$data = array('name'=>'tourists_offers', 'id'=>'tourists_offers', 'value'=>$this->validation->tourists_offers?$this->validation->tourists_offers:$content['DS']->tourists_offers, 'cols'=>'82', 'rows'=>'2');
			$content['tourists_offers_field'] =  form_textarea($data);;
			# } tourists_offers
			
			# { tourists_answers
			$content['tourists_answers'] = $this->RenderLabel('tourists_answers');
			$data = array('name'=>'tourists_answers', 'id'=>'tourists_answers', 'value'=>$this->validation->tourists_answers?$this->validation->tourists_answers:$content['DS']->tourists_answers, 'cols'=>'30', 'rows'=>'2');
			$content['tourists_answers_field'] =  form_textarea($data);
			# } tourists_answers
			
			# { phones
			$content['phones'] = $this->RenderLabel('phones');
			$data = array('name'=>'phones', 'id'=>'phones', 'value'=>$this->validation->phones?$this->validation->phones:$content['DS']->phones, 'size'=>'20');
			$content['phones_field'] =  form_input($data);
			# } phones
			
			# { email
			$content['email'] = $this->RenderLabel('email');
			$data = array('name'=>'email', 'id'=>'email', 'value'=>$this->validation->email?$this->validation->email:$content['DS']->email, 'size'=>'20');
			$content['email_field'] =  form_input($data);
			# } email
			
			# { date_from
			$content['date_from'] = $this->RenderLabel('date_from');
			$data = array('name'=>'date_from', 'id'=>'date_from', 'value'=>$this->validation->date_from?$this->validation->date_from:$content['DS']->date_from, 'size'=>'12', 'readonly'=>'readonly');		
			$content['date_from_field'] =  form_input($data).$this->RenderCalendar('date_from');
			# } date_from
			
			# { date_to
			$content['date_to'] = $this->RenderLabel('date_to');
			$data = array('name'=>'date_to', 'id'=>'date_to', 'value'=>$this->validation->date_to?$this->validation->date_to:$content['DS']->date_to, 'size'=>'12', 'readonly'=>'readonly');		
			$content['date_to_field'] =  form_input($data).$this->RenderCalendar('date_to');
			# } date_to
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('calls/edit', $content, True);
		}
		if($this->calls_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->calls_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT');
			
			# { date_doc
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$data = array('name'=>'date_doc', 'id'=>'date_doc', 'value'=>$this->validation->date_doc?$this->validation->date_doc:$content['DS']->date_doc, 'size'=>'12', 'readonly'=>'readonly');
			$content['date_doc_field'] =  form_input($data);
			# } date_doc
			
			# { _callsstatuses_rid
			$content['_callsstatuses_rid'] = $this->lang->line('CALLSTATUS');
			$content['_callsstatuses_rid_field'] =  form_dropdown('_callsstatuses_rid', $this->callsstatuses_model->GetPairArr(), $this->validation->_callsstatuses_rid?$this->validation->_callsstatuses_rid:$content['DS']->_callsstatuses_rid);
			# } _callsstatuses_rid
			
			# { _countries_rid
			$countries = array(' '=>'');
			foreach($this->calls_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid'] = $this->lang->line('COUNTRY'); 
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries, $this->validation->_countries_rid?$this->validation->_countries_rid:$content['DS']->_countries_rid);
			# } _countries_rid
			
			# { _callhotelcats_rid
			$hotelscats = array(' '=>'');
			foreach($this->calls_model->GetCallhotelcatsList() as $hotelcat){
				$hotelscats[$hotelcat->rid] = $hotelcat->callhotelcat_name;
			}
			$content['_callhotelcats_rid'] = $this->lang->line('HOTELCAT'); 
			$content['_callhotelcats_rid_field'] =  form_dropdown('_callhotelcats_rid', $hotelscats, $this->validation->_callhotelcats_rid?$this->validation->_callhotelcats_rid:$content['DS']->_callhotelcats_rid);
			# { _callhotelcats_rid
			
			# { _advertisessources_rid
			$content['_advertisessources_rid'] = $this->lang->line('ADVERTISE');
			$content['_advertisessources_rid_field'] =  form_dropdown('_advertisessources_rid', $this->advertisessources_model->GetPairArr(), $this->validation->_advertisessources_rid?$this->validation->_advertisessources_rid:$content['DS']->_advertisessources_rid);
			# } _advertisessources_rid
						
			# { _currencies_rid
			$content['_currencies_rid'] = $this->lang->line('CURRENCY'); 
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $this->currencies_model->GetPairArr(), $this->validation->_currencies_rid?$this->validation->_currencies_rid:$content['DS']->_currencies_rid);
			# } _currencies_rid
			
			# { f_name
			$content['f_name'] = $this->lang->line('F_NAME'); 
			$data = array('name'=>'f_name', 'id'=>'f_name', 'value'=>$this->validation->f_name?$this->validation->f_name:$content['DS']->f_name, 'size'=>'20');
			$content['f_name_field'] =  form_input($data);
			# } f_name
			
			# { s_name
			$content['s_name'] = $this->lang->line('S_NAME');
			$data = array('name'=>'s_name', 'id'=>'s_name', 'value'=>$this->validation->s_name?$this->validation->s_name:$content['DS']->s_name, 'size'=>'20');
			$content['s_name_field'] =  form_input($data);
			# } s_name
			
			# { l_name
			$content['l_name'] = $this->lang->line('L_NAME');
			$data = array('name'=>'l_name', 'id'=>'l_name', 'value'=>$this->validation->l_name?$this->validation->l_name:$content['DS']->l_name, 'size'=>'20');
			$content['l_name_field'] =  form_input($data);
			# } l_name
			
			# { summ_wanted_from
			$content['summ_wanted_from'] = $this->lang->line('SUMM_FROM'); 
			$data = array('name'=>'summ_wanted_from', 'id'=>'summ_wanted_from', 'value'=>$this->validation->summ_wanted_from?$this->validation->summ_wanted_from:$content['DS']->summ_wanted_from, 'size'=>'12');
			$content['summ_wanted_from_field'] =  form_input($data);
			# } summ_wanted_from
			
			# { summ_wanted_to
			$content['summ_wanted_to'] = $this->lang->line('SUMM_TO');
			$data = array('name'=>'summ_wanted_to', 'id'=>'summ_wanted_to', 'value'=>$this->validation->summ_wanted_to?$this->validation->summ_wanted_to:$content['DS']->summ_wanted_to, 'size'=>'12');
			$content['summ_wanted_to_field'] =  form_input($data);
			# } summ_wanted_to
			
			# { tourists_quan
			$content['tourists_quan'] = $this->lang->line('TOURISTS_QUAN');
			$data = array('name'=>'tourists_quan', 'id'=>'tourists_quan', 'value'=>$this->validation->tourists_quan?$this->validation->tourists_quan:$content['DS']->tourists_quan, 'size'=>'12');
			$content['tourists_quan_field'] =  form_input($data);
			# } tourists_quan
			
			# { tourists_wish
			$content['tourists_wish'] = $this->lang->line('TOURISTS_WISH');
			$data = array('name'=>'tourists_wish', 'id'=>'tourists_wish', 'value'=>$this->validation->tourists_wish?$this->validation->tourists_wish:$content['DS']->tourists_wish, 'cols'=>'30', 'rows'=>'2');
			$content['tourists_wish_field'] =  form_textarea($data);
			# } tourists_wish
			
			# { tourists_offers
			$content['tourists_offers'] = $this->lang->line('TOURISTS_OFFERS');
			$data = array('name'=>'tourists_offers', 'id'=>'tourists_offers', 'value'=>$this->validation->tourists_offers?$this->validation->tourists_offers:$content['DS']->tourists_offers, 'cols'=>'83', 'rows'=>'2');
			$content['tourists_offers_field'] =  form_textarea($data);;
			# } tourists_offers
			
			# { tourists_answers
			$content['tourists_answers'] = $this->lang->line('TOURISTS_ANSWERS');
			$data = array('name'=>'tourists_answers', 'id'=>'tourists_answers', 'value'=>$this->validation->tourists_answers?$this->validation->tourists_answers:$content['DS']->tourists_answers, 'cols'=>'30', 'rows'=>'2');
			$content['tourists_answers_field'] =  form_textarea($data);
			# } tourists_answers
			
			# { phones
			$content['phones'] = $this->RenderLabel('phones');
			$data = array('name'=>'phones', 'id'=>'phones', 'value'=>$this->validation->phones?$this->validation->phones:$content['DS']->phones, 'size'=>'20');
			$content['phones_field'] =  form_input($data);
			# } phones
			
			# { email
			$content['email'] = $this->RenderLabel('email');
			$data = array('name'=>'email', 'id'=>'email', 'value'=>$this->validation->email?$this->validation->email:$content['DS']->email, 'size'=>'20');
			$content['email_field'] =  form_input($data);
			# } email
			
			# { date_from
			$content['date_from'] = $this->lang->line('DATE_FROM'); 
			$data = array('name'=>'date_from', 'id'=>'date_from', 'value'=>$this->validation->date_from?$this->validation->date_from:$content['DS']->date_from, 'size'=>'12', 'readonly'=>'readonly');		
			$content['date_from_field'] =  form_input($data).$this->RenderCalendar('date_from');
			# } date_from
			
			# { date_to
			$content['date_to'] = $this->lang->line('DATE_TO');
			$data = array('name'=>'date_to', 'id'=>'date_to', 'value'=>$this->validation->date_to?$this->validation->date_to:$content['DS']->date_to, 'size'=>'12', 'readonly'=>'readonly');		
			$content['date_to_field'] =  form_input($data).$this->RenderCalendar('date_to');
			# } date_to
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('calls/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->calls_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
		if (!isset($_POST['goFind'])){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_FIND')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_FIND');
			
			# { date_doc_from
			$content['date_doc_from'] = $this->lang->line('DATE_DOC_FROM');
			$data = array('name'=>'date_doc_from', 'id'=>'date_doc_from', 'readonly'=>'readonly', 'size'=>'12'); 
			$content['date_doc_from_field'] = form_input($data).$this->RenderCalendar('date_doc_from');
			# } date_doc_from

			# { date_doc_to
			$content['date_doc_to'] = $this->lang->line('DATE_DOC_TO');
			$data = array('name'=>'date_doc_to', 'id'=>'date_doc_to', 'readonly'=>'readonly', 'size'=>'12'); 
			$content['date_doc_to_field'] = form_input($data).$this->RenderCalendar('date_doc_to');
			# } date_doc_to
			
			# { _callsstatuses_rid
			$content['_callsstatuses_rid'] = $this->lang->line('CALLSTATUS');
			$content['_callsstatuses_rid_field'] =  form_dropdown('_callsstatuses_rid', $this->callsstatuses_model->GetPairArr());
			# } _callsstatuses_rid
			
			# { _countries_rid
			$countries = array(' '=>'');
			foreach($this->calls_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid'] = $this->lang->line('COUNTRY'); 
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries);
			# } _countries_rid
			
			# { _callhotelcats_rid
			$hotelscats = array(' '=>'');
			foreach($this->calls_model->GetCallhotelcatsList() as $hotelcat){
				$hotelscats[$hotelcat->rid] = $hotelcat->callhotelcat_name;
			}
			$content['_callhotelcats_rid'] = $this->lang->line('HOTELCAT'); 
			$content['_callhotelcats_rid_field'] =  form_dropdown('_callhotelcats_rid', $hotelscats);
			# { _callhotelcats_rid
			
			# { _advertisessources_rid
			$content['_advertisessources_rid'] = $this->lang->line('ADVERTISE');
			$content['_advertisessources_rid_field'] =  form_dropdown('_advertisessources_rid', $this->advertisessources_model->GetPairArr());
			# } _advertisessources_rid
			
			# { f_name
			$content['f_name'] = $this->lang->line('F_NAME'); 
			$data = array('name'=>'f_name', 'id'=>'f_name', 'size'=>'20');
			$content['f_name_field'] =  form_input($data);
			# } f_name
			
			# { s_name
			$content['s_name'] = $this->lang->line('S_NAME');
			$data = array('name'=>'s_name', 'id'=>'s_name', 'size'=>'20');
			$content['s_name_field'] =  form_input($data);
			# } s_name
			
			# { l_name
			$content['l_name'] = $this->lang->line('L_NAME');
			$data = array('name'=>'l_name', 'id'=>'l_name', 'size'=>'20');
			$content['l_name_field'] =  form_input($data);
			# } l_name

			# { tourists_wish
			$content['tourists_wish'] = $this->lang->line('TOURISTS_WISH');
			$data = array('name'=>'tourists_wish', 'id'=>'tourists_wish', 'cols'=>'30', 'rows'=>'1');
			$content['tourists_wish_field'] =  form_textarea($data);
			# } tourists_wish
			
			# { tourists_offers
			$content['tourists_offers'] = $this->lang->line('TOURISTS_OFFERS');
			$data = array('name'=>'tourists_offers', 'id'=>'tourists_offers', 'cols'=>'83', 'rows'=>'1');
			$content['tourists_offers_field'] =  form_textarea($data);;
			# } tourists_offers
			
			# { tourists_answers
			$content['tourists_answers'] = $this->lang->line('TOURISTS_ANSWERS');
			$data = array('name'=>'tourists_answers', 'id'=>'tourists_answers', 'cols'=>'30', 'rows'=>'1');
			$content['tourists_answers_field'] =  form_textarea($data);
			# } tourists_answers
			
			# { phones
			$content['phones'] = $this->RenderLabel('phones');
			$data = array('name'=>'phones', 'id'=>'phones', 'size'=>'30');
			$content['phones_field'] =  form_input($data);
			# } phones
			
			# { email
			$content['email'] = $this->RenderLabel('email');
			$data = array('name'=>'email', 'id'=>'email', 'size'=>'20');
			$content['email_field'] =  form_input($data);
			# } email
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2'); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array(' '=>'', '0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')));
			# } archive
			return $this->load->view('calls/find', $content, True);			
		}
		$searchRule = array();
		if(isset($_POST['date_doc_from']) && trim($_POST['date_doc_from'])) $searchRule['_calls_headers.date_doc>='] = trim($_POST['date_doc_from']);
		if(isset($_POST['date_doc_to']) && trim($_POST['date_doc_to'])) $searchRule['_calls_headers.date_doc<='] = trim($_POST['date_doc_to']);
		if(isset($_POST['_callsstatuses_rid']) && trim($_POST['_callsstatuses_rid'])) $searchRule['_calls_headers._callsstatuses_rid'] = trim($_POST['_callsstatuses_rid']);
		if(isset($_POST['_advertisessources_rid']) && trim($_POST['_advertisessources_rid'])) $searchRule['_calls_rows._advertisessources_rid'] = trim($_POST['_advertisessources_rid']);
		if(isset($_POST['_callhotelcats_rid']) && trim($_POST['_callhotelcats_rid'])) $searchRule['_calls_rows._callhotelcats_rid'] = trim($_POST['_callhotelcats_rid']);
		if(isset($_POST['f_name']) && trim($_POST['f_name'])) $searchRule['_calls_rows.f_name'] = trim($_POST['f_name']);
		if(isset($_POST['s_name']) && trim($_POST['s_name'])) $searchRule['_calls_rows.s_name'] = trim($_POST['s_name']);
		if(isset($_POST['l_name']) && trim($_POST['l_name'])) $searchRule['_calls_rows.l_name'] = trim($_POST['l_name']);
		if(isset($_POST['tourists_wish']) && trim($_POST['tourists_wish'])) $searchRule['_calls_rows.tourists_wish'] = trim($_POST['tourists_wish']);
		if(isset($_POST['tourists_offers']) && trim($_POST['tourists_offers'])) $searchRule['_calls_rows.tourists_offers'] = trim($_POST['tourists_offers']);
		if(isset($_POST['tourists_answers']) && trim($_POST['tourists_answers'])) $searchRule['_calls_rows.tourists_answers'] = trim($_POST['tourists_answers']);
		if(isset($_POST['phones']) && trim($_POST['phones'])) $searchRule['_calls_rows.phones'] = trim($_POST['phones']);
		if(isset($_POST['email']) && trim($_POST['email'])) $searchRule['_calls_rows.email'] = trim($_POST['email']);
		if(isset($_POST['descr']) && trim($_POST['descr'])) $searchRule['_documents.descr'] = trim($_POST['descr']);
		if(isset($_POST['archive']) && trim($_POST['archive'])) $searchRule['_documents.archive'] = trim($_POST['archive']);
		if($searchRule){
			$this->_setsearchrule($searchRule);
		}
		if($_POST['goFind']=='2'){
			redirect('calls', 'refresh');
		}
		$this->load->view('standart/tmpl/find_success', array('identifier'=>$this->GetIdentifier()));
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
	
	private function _gdemand(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$content['DS'] = $this->calls_model->GetEditDS();
		redirect('demands', 'refresh');
	}
}

?>